package com.yoo.ons.function.profile.viewmodel

import com.framework.network.remote.NetworkObserver
import com.framework.network.remote.endRequest
import com.framework.network.remote.error
import com.framework.network.remote.requestObserver
import com.framework.network.remote.startRequest
import com.framework.network.remote.success
import com.yoo.module_base.view.tag.TagModel
import com.yoo.ons.base.BaseVMViewModel
import com.yoo.ons.base.HttpRequestListener
import com.yoo.ons.base.Request
import com.yoo.ons.model.PictureManagerModel
import com.yoo.ons.model.UserInfoModel

class EditProfileViewModel : BaseVMViewModel() {

    fun saveProfileInfo(listener: HttpRequestListener<UserInfoModel>) {
        request(listener,request=object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postChangeUserInfo(map)?.requestObserver(
                    NetworkObserver<UserInfoModel>().startRequest {
                        showLoading()
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        hideLoading()
                        requestEndListener?.invoke()
                    }
                )
            }

        })
    }

    fun getChangeImage(listener: HttpRequestListener<PictureManagerModel>) {
        request(listener,request=object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postChangeImage(map)?.requestObserver(
                    NetworkObserver<PictureManagerModel>().startRequest {
                        showLoading()
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        hideLoading()
                        requestEndListener?.invoke()
                    }
                )
            }

        })
    }

    fun getUserInterests(listener: HttpRequestListener<ArrayList<TagModel>>) {
        request(listener,request=object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postUserInterests(map)?.requestObserver(
                    NetworkObserver<ArrayList<TagModel>>().startRequest {
                        showLoading()
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        hideLoading()
                        requestEndListener?.invoke()
                    }
                )
            }

        })
    }

}